package com.example.tang.zhayan.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tang.zhayan.BaseApp;
import com.example.tang.zhayan.R;
import com.example.tang.zhayan.bean.DbBuyDetail;
import com.squareup.picasso.Picasso;

import org.simple.eventbus.EventBus;
import org.w3c.dom.Text;
import org.xutils.ex.DbException;

import java.util.List;

/**
 * Created by tang on 2015/12/19.
 */
public class ShopBagAdapter extends BaseAbstractAdapter<DbBuyDetail> {

    private ViewHolder holder = null;

    public ShopBagAdapter(Context context, List<DbBuyDetail> list) {
        super(context, list);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        holder = null;
        if (holder == null) {
            convertView = inflater.inflate(R.layout.item_shop_bag_layout, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Picasso.with(context).load(list.get(position).getImgUrl()).
                placeholder(R.mipmap.default_loader_image1).into(holder.img);
        holder.textName.setText(list.get(position).getName());
        holder.textPrice.setText(list.get(position).getPrice());
        holder.textColor.setText("颜色：" + list.get(position).getColor());
        holder.textSize.setText("尺码：" + list.get(position).getSize());
        holder.textNum.setText(list.get(position).getCurrentCount());
        holder.textOffice.setText(list.get(position).getInfo());
        holder.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DbBuyDetail dbBuyDetail = list.get(position);
                if (Integer.parseInt(dbBuyDetail.getCurrentCount()) < Integer.parseInt(dbBuyDetail.getTotalCount())) {
                    dbBuyDetail.setCurrentCount((Integer.parseInt(list.get(position).getCurrentCount()) + 1) + "");
                    try {
                        BaseApp.getApp().getDb().saveOrUpdate(dbBuyDetail);
                        notifyDataSetChanged();
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                } else {
                    Toast.makeText(context, "天呐，惊现土豪一枚~可惜我没有货了啊", Toast.LENGTH_SHORT).show();
                }
            }
        });
        holder.ivLess.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DbBuyDetail dbBuyDetail = list.get(position);
                if (Integer.parseInt(dbBuyDetail.getCurrentCount()) > 0) {
                    dbBuyDetail.setCurrentCount((Integer.parseInt(list.get(position).getCurrentCount()) - 1) + "");
                    try {
                        BaseApp.getApp().getDb().saveOrUpdate(dbBuyDetail);
                        notifyDataSetChanged();
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                } else {
                    Toast.makeText(context, "你想把你的衣服也送给我吗~亲", Toast.LENGTH_SHORT).show();
                }
            }
        });
        //如果flag = 1   checkBox就设置为选中
        if (list.get(position).getFlag() == 1) {
            holder.cbChecked.setChecked(true);
        }
        holder.cbChecked.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //刷新数据库里面的状态
                    list.get(position).setFlag(1);
                    try {
                        BaseApp.getApp().getDb().saveOrUpdate(list.get(position));
                        notifyDataSetChanged();
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                }else{
                    list.get(position).setFlag(0);
                    try {
                        BaseApp.getApp().getDb().saveOrUpdate(list.get(position));
                        notifyDataSetChanged();
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        return convertView;
    }


    class ViewHolder {
        ImageView img;
        TextView textName;
        TextView textSize;
        TextView textPrice;
        TextView textColor;
        TextView textNum;
        TextView textOffice;
        ImageView ivAdd;
        ImageView ivLess;
        CheckBox cbChecked;

        public ViewHolder(View convertView) {
            img = (ImageView) convertView.findViewById(R.id.iv_clothes);
            textName = (TextView) convertView.findViewById(R.id.tv_info);
            textSize = (TextView) convertView.findViewById(R.id.tv_size);
            textPrice = (TextView) convertView.findViewById(R.id.tv_price);
            textColor = (TextView) convertView.findViewById(R.id.tv_color);
            textOffice = (TextView) convertView.findViewById(R.id.tv_office_left);
            textNum = (TextView) convertView.findViewById(R.id.tv_count);
            ivAdd = (ImageView) convertView.findViewById(R.id.iv_add);
            ivLess = (ImageView) convertView.findViewById(R.id.iv_delete);
            cbChecked = (CheckBox) convertView.findViewById(R.id.cb_shop_bg);
        }

    }
}
